package com.flqy.baselibrary;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.core.util.Pair;
import androidx.viewpager.widget.ViewPager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.ListView;

import com.alexvasilkov.gestures.GestureController;
import com.alexvasilkov.gestures.animation.ViewPosition;
import com.alexvasilkov.gestures.animation.ViewPositionAnimator;
import com.alexvasilkov.gestures.commons.RecyclePagerAdapter;
import com.alexvasilkov.gestures.views.GestureImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.flqy.baselibrary.entity.PhotoItem;
import com.flqy.baselibrary.utils.GlideHelper;
import com.flqy.baselibrary.utils.ImageUtils;
import com.flqy.baselibrary.utils.ViewUtils;
import com.flqy.baselibrary.widget.HackyViewPager;
import com.flqy.baselibrary.widget.PagerIndicator;
import com.gyf.immersionbar.ImmersionBar;

import java.util.ArrayList;
import java.util.List;


public class PhotoPagerActivity extends Activity {
    private static final long PROGRESS_DELAY = 300L;
    public static final String EXTRA_VIEW_POSITIONS = "extra_view_positions";
    public static final String EXTRA_PHOTO_URIS = "photoUris";
    public static final String EXTRA_CLICK_POSITION = "position";
    public static final String EXTRA_PHOTOS = "photos";
    public static final String EXTRA_FIRST_VIEW_POSITION = "extra_first_view_position";
    public static final String EXTRA_TOTAL_COUNT = "totalCount";
    public static final String EXTRA_CHECKABLE = "checkable";
    public static final String EXTRA_MAX_CHECK_COUNT = "maxCheckCount";
    public static final String EXTRA_HIDE_STATUS_BAR = "hide_status_bar";
    public static final String EXTRA_RESULT = "result";
    private HackyViewPager mViewPager;

    private PhotoPagerAdapter adapter;
    private PagerIndicator photoIndicator;

    private int startPosition;
    private View contentView;
    private ArrayList<PhotoItem> photoItems;
    private ArrayList<PhotoItem> checkedPhotoItems;
    private int firstViewPosition;
    private List<ViewPosition> viewPositions;
    private boolean enter = false;
    private GestureImageView currentImage;
    private int totalCount;
    private FrameLayout footerLayout;
    private CheckBox photoCheckedStatus;
    private boolean checkable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ImmersionBar.with(this).transparentNavigationBar().transparentStatusBar().init();
        boolean hideStatusBar = getIntent().getBooleanExtra(EXTRA_HIDE_STATUS_BAR, true);
        if (hideStatusBar) {
            //取消状态栏
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
//            bar.fullScreen(true);
        }
        if (Build.VERSION.SDK_INT != Build.VERSION_CODES.O) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        setContentView(getContentViewId());
        initView();
    }

    protected int getContentViewId() {
        return R.layout.activity_photos_pager;
    }

    protected void initView() {
        contentView = findViewById(R.id.contentView);
        mViewPager = (HackyViewPager) findViewById(R.id.hackpager);
        photoIndicator = (PagerIndicator) findViewById(R.id.photoIndicator);
        footerLayout = (FrameLayout) findViewById(R.id.footerLayout);
        photoCheckedStatus = (CheckBox) findViewById(R.id.photoCheckedStatus);
        handleIntent(getIntent());
    }

    @Override
    public void finish() {

        if (checkable) {
            Intent extra = new Intent();
            ArrayList<String> checkedList = new ArrayList<>();
            for (PhotoItem item : checkedPhotoItems) {
                checkedList.add(item.getUri());
            }
            extra.putExtra(EXTRA_RESULT, checkedList);
            setResult(RESULT_OK, extra);
        }
        super.finish();
        if (currentImage == null) {
            contentView.setBackgroundColor(Color.BLACK);
            overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
        } else {
            overridePendingTransition(0, 0);
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        handleIntent(intent);
    }

    @SuppressWarnings("deprecation")
    private void handleIntent(Intent intent) {
        ArrayList<String> photoUris = intent.getStringArrayListExtra(EXTRA_PHOTO_URIS);
        startPosition = intent.getIntExtra(EXTRA_CLICK_POSITION, 0);
        firstViewPosition = intent.getIntExtra(EXTRA_FIRST_VIEW_POSITION, 0);
        totalCount = intent.getIntExtra(EXTRA_TOTAL_COUNT, 0);
        ArrayList<String> positions = intent.getStringArrayListExtra(EXTRA_VIEW_POSITIONS);
        if (positions != null) {
            viewPositions = new ArrayList<>();
            for (String position : positions) {
                viewPositions.add(ViewPosition.unpack(position));
            }
        }
        photoItems = intent.getParcelableArrayListExtra(EXTRA_PHOTOS);

        if (photoUris != null) {
            photoItems = new ArrayList<>();
            for (String photoUri : photoUris) {
                PhotoItem item = new PhotoItem();
                item.setId(photoUri);
                item.setUri(photoUri);
                photoItems.add(item);
            }
        }

        adapter = new PhotoPagerAdapter(mViewPager, photoItems);
        mViewPager.setAdapter(adapter);
        photoIndicator.setViewPager(mViewPager);
        mViewPager.setCurrentItem(startPosition);

//        mViewPager.setPageTransformer(true, new DepthPageTransformer());
        if (viewPositions == null || viewPositions.size() == 0) {
            overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
            contentView.setBackgroundColor(Color.BLACK);
        } else {
            overridePendingTransition(0, 0);
            photoIndicator.setAlpha(0);
            footerLayout.setAlpha(0);
            photoCheckedStatus.setAlpha(0);
        }

        setCheckedStatus();

        if (checkable) {
            ViewUtils.paddingToNavigationBar(this,contentView);
        } else {
            ViewUtils.paddingToNavigationBar(this,(View) photoIndicator.getParent());
        }
    }

    private void setCheckedStatus() {
        checkable = getIntent().getBooleanExtra(EXTRA_CHECKABLE, false);
        final int maxCheckCount = getIntent().getIntExtra(EXTRA_MAX_CHECK_COUNT, photoItems.size());
        if (checkable) {
            footerLayout.setVisibility(View.VISIBLE);
            checkedPhotoItems = new ArrayList<>();
            checkedPhotoItems.addAll(photoItems);
            photoCheckedStatus.setChecked(true);
            photoCheckedStatus.setText(String.format(getString(R.string.current_checked), checkedPhotoItems.size(), maxCheckCount));
            photoCheckedStatus.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PhotoItem item = photoItems.get(mViewPager.getCurrentItem());
                    if (checkedPhotoItems.contains(item)) {
                        checkedPhotoItems.remove(item);
                        photoCheckedStatus.setChecked(false);
                    } else {
                        checkedPhotoItems.add(item);
                        photoCheckedStatus.setChecked(true);
                    }
                    photoCheckedStatus.setText(String.format(getString(R.string.current_checked), checkedPhotoItems.size(), maxCheckCount));

                }
            });

            mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    PhotoItem item = photoItems.get(position);
                    if (checkedPhotoItems.contains(item)) {
                        photoCheckedStatus.setChecked(true);
                    } else {
                        photoCheckedStatus.setChecked(false);
                    }

                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
        }
    }

    @Override
    public void onBackPressed() {
        if (currentImage != null) {
            exist(currentImage);
        } else {
            finish();
        }
    }

    private void exist(final GestureImageView image) {
        if (isPositionCorrect(image) && !image.getPositionAnimator().isLeaving()) {
            image.getPositionAnimator().exit(true);
            image.getPositionAnimator().addPositionUpdateListener(new ViewPositionAnimator.PositionUpdateListener() {
                @Override
                public void onPositionUpdate(float position, boolean isLeaving) {
                    String value = Integer.toHexString((int) (255 * position));
                    photoIndicator.setAlpha(getIndicatorAlpha(position));
                    footerLayout.setAlpha(getIndicatorAlpha(position));
                    photoCheckedStatus.setAlpha(getIndicatorAlpha(position));
                    if (value.length() == 1) {
                        value = "0" + value;
                    }
                    contentView.setBackgroundColor(Color.parseColor("#" + value + "000000"));
                    if (position == 0) {
                        image.getPositionAnimator().removePositionUpdateListener(this);
                    }
                }
            });

        }
    }

    private float getIndicatorAlpha(float position) {
        return position * position * position;
    }

    private static boolean isPositionCorrect(GestureImageView image) {
        boolean isPositionCorrect = image.getPositionAnimator().getPosition() > 0 && image.getPositionAnimator().getPosition() <= 1;
        return isPositionCorrect;
    }


    protected class PhotoPagerAdapter extends RecyclePagerAdapter<PhotoPagerAdapter.ViewHolder> {


        private boolean isExitEnabled = false;
        private boolean isFillViewport = true;
        private ArrayList<PhotoItem> photoItems;
        private ViewPager viewPager;

        public PhotoPagerAdapter(ViewPager pager, ArrayList<PhotoItem> photoItems) {
            this.viewPager = pager;
            this.photoItems = photoItems;
        }

        @Override
        public int getCount() {
            return Math.max(totalCount, photoItems.size());
        }


        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup container) {
            final ViewHolder holder = new ViewHolder(container);

            holder.image.getController().getSettings()
                    .setExitEnabled(isExitEnabled)
                    .setFillViewport(isFillViewport)
                    .setMaxZoom(3);
            holder.image.getPositionAnimator().addPositionUpdateListener(new ViewPositionAnimator.PositionUpdateListener() {
                @Override
                public void onPositionUpdate(float position, boolean isLeaving) {
                    holder.progress.setVisibility(position == 1f ? View.VISIBLE : View.INVISIBLE);
                }
            });
            holder.image.getPositionAnimator().addPositionUpdateListener(new ViewPositionAnimator.PositionUpdateListener() {
                @Override
                public void onPositionUpdate(float position, boolean isLeaving) {

                    if (position == 0f && isLeaving) { // Exit finished
                        holder.image.getController().getSettings().disableBounds();
                        holder.image.getPositionAnimator().setState(0f, false, false);
                        // Finishing activity
                        finish();
                    }
                }
            });
            holder.image.getController().enableScrollInViewPager(viewPager);
            holder.image.getController().setOnGesturesListener(new GestureController.OnGestureListener() {
                @Override
                public void onDown(@NonNull MotionEvent event) {

                }

                @Override
                public void onUpOrCancel(@NonNull MotionEvent event) {

                }

                @Override
                public boolean onSingleTapUp(@NonNull MotionEvent event) {
                    return false;
                }

                @Override
                public boolean onSingleTapConfirmed(@NonNull MotionEvent event) {
                    exist(holder.image);
                    return false;
                }


                @Override
                public void onLongPress(@NonNull MotionEvent event) {

                }

                @Override
                public boolean onDoubleTap(@NonNull MotionEvent event) {
                    return false;
                }
            });

            return holder;
        }

        @Override
        public void setPrimaryItem(ViewGroup container, int position, Object object) {
            super.setPrimaryItem(container, position, object);
            ViewHolder holder = (ViewHolder) object;
            if (holder != null) {
                currentImage = holder.image;
            }
        }


        @Override
        public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
            if (!holder.gesturesDisabled) {
                holder.image.getController().getSettings().disableGestures();
                holder.gesturesDisabled = true;
            }
            holder.image.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    //长按
                        new AlertDialog.Builder(PhotoPagerActivity.this)
                            .setMessage("保存图片？")
                            .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    ImageUtils.saveScreenShot(PhotoPagerActivity.this,ImageUtils.takeViewShot(holder.image));
                                }
                            }).show();
                    return true;
                }
            });
            holder.progress.animate().setStartDelay(PROGRESS_DELAY).alpha(1f);
            if (photoItems.size() > position) {
                // Loading image
                GlideHelper.loadFlickrFull(photoItems.get(position).getUri(), photoItems.get(position).getThumbUri(), holder.image,
                        new GlideHelper.ImageLoadingListener() {
                            @Override
                            public void onLoaded() {
                                holder.progress.animate().cancel();
                                holder.progress.setAlpha(0f);
                                // Re-enabling touch controls
                                if (holder.gesturesDisabled) {
                                    holder.image.getController().getSettings().enableGestures();
                                    holder.gesturesDisabled = false;
                                }
                            }

                            @Override
                            public void onFailed() {
                                holder.progress.animate().alpha(0f);
                            }
                        });
            }
            if (!enter && startPosition == position) {
                holder.image.getPositionAnimator().addPositionUpdateListener(new ViewPositionAnimator.PositionUpdateListener() {
                    @Override
                    public void onPositionUpdate(float position, boolean isLeaving) {
                        enter = true;
                        String value = Integer.toHexString((int) (255 * position));
                        photoIndicator.setAlpha(getIndicatorAlpha(position));
                        footerLayout.setAlpha(getIndicatorAlpha(position));
                        photoCheckedStatus.setAlpha(getIndicatorAlpha(position));
                        if (value.length() == 1) {
                            value = "0" + value;
                        }
                        contentView.setBackgroundColor(Color.parseColor("#" + value + "000000"));
                        if (position == 1) {
                            holder.image.getPositionAnimator().removePositionUpdateListener(this);
                        }
                    }
                });

            }

            boolean animate = (position == startPosition);
            int currentViewPositionIndex = position - firstViewPosition;
            if (viewPositions != null && currentViewPositionIndex >= 0 && currentViewPositionIndex < viewPositions.size()) {
                holder.image.getPositionAnimator().enter(viewPositions.get(currentViewPositionIndex), animate);
            } else {
                holder.image.getPositionAnimator().enter(animate);
            }

        }

        @Override
        public void onRecycleViewHolder(@NonNull ViewHolder holder) {
            super.onRecycleViewHolder(holder);

            if (holder.gesturesDisabled) {
                holder.image.getController().getSettings().enableGestures();
                holder.gesturesDisabled = false;
            }

            Glide.with(PhotoPagerActivity.this).clear(holder.image);

            holder.progress.animate().cancel();
            holder.progress.setAlpha(0f);

            holder.image.setImageDrawable(null);
        }

        class ViewHolder extends RecyclePagerAdapter.ViewHolder {
            final GestureImageView image;
            final View progress;
            boolean gesturesDisabled;

            ViewHolder(ViewGroup parent) {
                super(ViewUtils.inflate(parent, R.layout.item_photo_full));
                image = ViewUtils.find(itemView, R.id.photo_full_image);
                progress = ViewUtils.find(itemView, R.id.photo_full_progress);
            }
        }


    }


    public static void launchWithPhotos(Context context, List<View> visibleViews, ArrayList<PhotoItem> photos, Pair<View, Integer> clickViewAndPhotoPosition, int totalCount) {
        Intent intent = new Intent(context, PhotoPagerActivity.class);
        ArrayList<String> viewPositions = new ArrayList<>();
        for (int i = 0; i < visibleViews.size(); i++) {
            View v = visibleViews.get(i);
            viewPositions.add(ViewPosition.from(v).pack());
            if (v == clickViewAndPhotoPosition.first) {
                intent.putExtra(EXTRA_FIRST_VIEW_POSITION, Math.max(clickViewAndPhotoPosition.second - i, 0));
            }
        }
        intent.putParcelableArrayListExtra(EXTRA_PHOTOS, photos);
        intent.putExtra(EXTRA_VIEW_POSITIONS, viewPositions);
        intent.putExtra(EXTRA_CLICK_POSITION, clickViewAndPhotoPosition.second);
        intent.putExtra(EXTRA_TOTAL_COUNT, totalCount);
        context.startActivity(intent);
    }

    public static void launchWithPhotos(Context context, ListView parent, ArrayList<PhotoItem> photos, int position, ImageGetterFromItemView getter) {
        Pair<List<View>, View> viewsAndTarget = getViewsAt(parent, position, getter);
        launchWithPhotos(context, viewsAndTarget.first, photos, new Pair<View, Integer>(viewsAndTarget.second, position), photos.size());
    }

    public static void launchWithPhotos(Context context, RecyclerView parent, ArrayList<PhotoItem> photos, int position, ImageGetterFromItemView getter, int totalCount) {
        Pair<List<View>, View> viewsAndTarget = getViewsAt(parent, position, getter);
        launchWithPhotos(context, viewsAndTarget.first, photos, new Pair<View, Integer>(viewsAndTarget.second, position), totalCount);
    }

    public static void launchWithPhotos(Context context, GridView parent, ArrayList<PhotoItem> photos, int position, ImageGetterFromItemView getter) {
        Pair<List<View>, View> viewsAndTarget = getViewsAt(parent, position, getter);
        launchWithPhotos(context, viewsAndTarget.first, photos, new Pair<View, Integer>(viewsAndTarget.second, position), photos.size());
    }

    public static void launchWithPhotos(Context context, GridLayout parent, ArrayList<PhotoItem> photos, int position, ImageGetterFromItemView getter) {
        List<View> childViews = new ArrayList<>();
        View clickedView = null;
        for (int i = 0; i < parent.getChildCount(); i++) {
            View v = getter.getImage(i, parent.getChildAt(i));
            if (v != null) {
                childViews.add(v);
                if (position == childViews.size() - 1) {
                    clickedView = v;
                }
            }
        }

        launchWithPhotos(context, childViews, photos, new Pair<View, Integer>(clickedView, position), photos.size());
    }

    public static void launchWithPhotos(Context context, GridLayout parent, ArrayList<PhotoItem> photos, int position, String userId) {

        launchWithPhotos(context, parent, photos, position, new ImageGetterFromItemView() {
            @Override
            public View getImage(int position, View itemView) {
                return itemView;
            }
        });
    }
    public static void launch(Context context, String photoUri) {
        ArrayList<String> uris=new ArrayList<>();
        uris.add(photoUri);
        launch(context,uris);
    }
    public static void launch(Context context, ArrayList<String> photoUris) {
        launch(context, null, photoUris, new Pair<View, Integer>(null, 0));
    }

    public static void launch(Context context, List<View> visibleViews, ArrayList<String> photoUris, Pair<View, Integer> viewAndPicPosition) {
        Intent intent = new Intent(context, PhotoPagerActivity.class);
        if (visibleViews != null) {
            ArrayList<String> viewPositions = new ArrayList<>();
            for (int i = 0; i < visibleViews.size(); i++) {
                View v = visibleViews.get(i);
                viewPositions.add(ViewPosition.from(v).pack());
                if (v == viewAndPicPosition.first) {
                    intent.putExtra(EXTRA_FIRST_VIEW_POSITION, Math.max(viewAndPicPosition.second - i, 0));
                }
            }
            intent.putExtra(EXTRA_VIEW_POSITIONS, viewPositions);
        }
        intent.putStringArrayListExtra(EXTRA_PHOTO_URIS, photoUris);

        intent.putExtra(EXTRA_CLICK_POSITION, viewAndPicPosition.second);
        context.startActivity(intent);
    }


    public interface ImageGetterFromItemView {
        public View getImage(int position, View itemView);
    }

    private static Pair<List<View>, View> getViewsAt(GridView parent, int position, ImageGetterFromItemView getter) {
        int first = parent.getFirstVisiblePosition();
        int last = parent.getLastVisiblePosition();
        ArrayList<View> views = new ArrayList<>();
        View clickView = null;
        for (int i = first; i <= last; i++) {
            View itemView = parent.getChildAt(i);
            if (itemView == null) {
                continue;
            }
            View v = getter.getImage(i, itemView);
            if (v != null) {
                views.add(v);
                if (first + views.size() - 1 == position) {//5
                    clickView = v;
                }
            }
        }
        return new Pair<List<View>, View>(views, clickView);

    }

    private static Pair<List<View>, View> getViewsAt(ListView parent, int position, ImageGetterFromItemView getter) {
        int first = parent.getFirstVisiblePosition();
        int last = parent.getLastVisiblePosition();
        ArrayList<View> views = new ArrayList<>();
        View clickView = null;
        for (int i = first; i <= last; i++) {
            View itemView = parent.getChildAt(i);
            if (itemView == null) {
                continue;
            }
            View v = getter.getImage(i, itemView);
            if (v != null) {
                views.add(v);
                if (first + views.size() - 1 == position) {//5
                    clickView = v;
                }
            }
        }
        return new Pair<List<View>, View>(views, clickView);

    }

    private static Pair<List<View>, View> getViewsAt(RecyclerView parent, int position, ImageGetterFromItemView getter) {
        LinearLayoutManager manager = (LinearLayoutManager) parent.getLayoutManager();
        int first = manager.findFirstVisibleItemPosition();
        int last = manager.findLastVisibleItemPosition();
        ArrayList<View> views = new ArrayList<>();
        View clickView = null;
        for (int i = first; i <= last; i++) {
            View itemView = parent.getLayoutManager().findViewByPosition(i);
            if (itemView == null) {
                continue;
            }
            View v = getter.getImage(i, itemView);
            if (v != null) {
                views.add(v);
                if (first + views.size() - 1 == position) {
                    clickView = v;
                }
            }

        }
        return new Pair<List<View>, View>(views, clickView);

    }

    public static void launch(Context context, ListView parent, ArrayList<String> photoUris, int position, ImageGetterFromItemView getter) {
        Pair<List<View>, View> viewsAndTarget = getViewsAt(parent, position, getter);
        launch(context, viewsAndTarget.first, photoUris, new Pair<View, Integer>(viewsAndTarget.second, position));
    }

    public static void launch(Context context, GridView parent, ArrayList<String> photoUris, int position, ImageGetterFromItemView getter) {
        Pair<List<View>, View> viewsAndTarget = getViewsAt(parent, position, getter);
        launch(context, viewsAndTarget.first, photoUris, new Pair<View, Integer>(viewsAndTarget.second, position));
    }

    public static void launch(Context context, RecyclerView parent, ArrayList<String> photoUris, int position, ImageGetterFromItemView getter) {
        Pair<List<View>, View> viewsAndTarget = getViewsAt(parent, position, getter);
        launch(context, viewsAndTarget.first, photoUris, new Pair<View, Integer>(viewsAndTarget.second, position));
    }

    public static void launch(Context context, GridLayout parent, ArrayList<String> photoUris, int position, ImageGetterFromItemView getter) {
        List<View> childViews = new ArrayList<>();
        View clickedView = null;
        for (int i = 0; i < parent.getChildCount(); i++) {
            View v = getter.getImage(i, parent.getChildAt(i));
            if (v != null) {
                childViews.add(v);
                if (position == childViews.size() - 1) {
                    clickedView = childViews.get(position);
                }
            }
        }
        launch(context, childViews, photoUris, new Pair<View, Integer>(clickedView, position));
    }

    public static void launch(Context context, GridLayout parent, ArrayList<String> photoUris, int position) {
        launch(context, parent, photoUris, position, new ImageGetterFromItemView() {
            @Override
            public View getImage(int position, View itemView) {
                return itemView;
            }
        });
    }

    public static void launch(Context context, View v, ArrayList<String> photoUris, int position) {
        ArrayList<View> views = new ArrayList<>();
        views.add(v);
        launch(context, views, photoUris, new Pair<View, Integer>(v, position));
    }

    public static void launch(Context context, View v, String url) {

        ArrayList<String> urls = new ArrayList<>();
        urls.add(url);
        launch(context, v, urls, 0);
    }

    protected void setTranslucentStatus(boolean on) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window win = getWindow();
            WindowManager.LayoutParams winParams = win.getAttributes();
            final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
            final int bitsNav = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
            if (on) {
                winParams.flags |= bits;
                winParams.flags |= bitsNav;
            } else {
                winParams.flags &= ~bits;
                winParams.flags &= ~bitsNav;
            }

            win.setAttributes(winParams);
        }
    }


}
